<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title>作家管理系统-小说精品屋</title>
        <link rel="stylesheet" href="/static/css/base.css?v=1" />
        <link rel="stylesheet" href="/static/css/user.css" />
        <style type="text/css">
            .redBtn {
                padding: 5px;
                border-radius: 20px;
                border: 1px solid #f80;
                background: #f80;
                color: #fff;
            }

            a.redBtn:hover {

                color: #fff;
            }
        </style>
    </head>
</head>

<body class="">

    <div class="header">
        <div class="mainNav" id="mainNav">
            <div class="box_center cf"
                style="text-align: center;height: 44px;line-height: 48px;color: #fff;font-size: 16px;">

                小说精品屋作家管理

            </div>
        </div>
    </div>

    <div class="main box_center cf">
        <div class="userBox cf">
            <div class="my_l">

                <ul class="log_list">
                    <li><a class="link_4 on" href="/author/index.html">小说管理</a></li>
                    <li><a class="link_2 " href="/author/author_income_detail.html">稿费收入</a></li>
                    <!-- <li><a class="link_1 " href="/user/userinfo.html">批量小说爬取</a></li>
                 <li><a class="link_4 " href="/user/favorites.html">单本小说爬取</a></li>-->
                </ul>

            </div>
            <div class="my_r">
                <div id="noContentDiv">
                    <div class="tc" style="margin-top: 200px"><a href="/author/book_add.html" class="btn_red">创建作品</a>
                    </div>

                </div>
                <div class="my_bookshelf" id="hasContentDiv" style="display: none">
                    <div class="title cf">
                        <h2 class="fl">小说列表</h2>
                        <div class="fr"><a href="/author/book_add.html" class="btn_red">发布小说</a></div>
                    </div>

                    <div id="divData" class="updateTable">
                        <table cellpadding="0" cellspacing="0">
                            <thead>
                                <tr>
                                    <th class="goread">
                                        书名
                                    </th>
                                    <th class="goread">
                                        分类
                                    </th>
                                    <th class="goread">
                                        点击量
                                    </th>
                                    <th class="goread">
                                        昨日订阅数
                                    </th>
                                    <th class="goread">
                                        更新时间
                                    </th>
                                    <th class="goread">
                                        总字数
                                    </th>
                                    <th class="goread">
                                        操作
                                    </th>
                                </tr>
                            </thead>
                            <tbody id="bookList">



                            </tbody>
                        </table>
                        <div class="pageBox cf" id="shellPage">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


</body>
<script src="/static/javascript/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="/static/javascript/config.js" type="text/javascript"></script>
<script src="/static/javascript/ajaxfileupload.js" type="text/javascript"></script>
<script src="/static/layui/layui.all.js" type="text/javascript"></script>
<script src="/static/javascript/header.js" type="text/javascript"></script>
<script src="/static/javascript/user.js" type="text/javascript"></script>
<script src="/static/javascript/date.js" type="text/javascript"></script>
<script src="/static/javascript/common.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">
    search(1, 5);

    function search(curr, limit) {

        $.ajax({
            type: "get",
            url: "/author/listBookByPage",
            data: { 'page': curr, 'limit': limit },
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    var bookList = data.data;
                    if (bookList.length > 0) {
                        $("#hasContentDiv").css("display", "block");
                        $("#noContentDiv").css("display", "none");
                        var bookListHtml = "";
                        for (var i = 0; i < bookList.length; i++) {
                            var book = bookList[i];
                            bookListHtml +=
                                `
                            <tr class="book_list" vals="291">
                                <td style="position: relative" class="goread">
                                    <input class="opacity" onchange="picChange('${book.id}')" type="file" id="file${book.id}" name="file" title="点击上传图片" style="z-index: 100;cursor: pointer;left: 30px; top: 0px; width: 60px; height: 80px; opacity: 0; position: absolute;  ">
                                    <img width="50" height="70" src="${book.pic_url}">
                                    <br>${book.book_name}
                                </td>
                                <td class="goread">${book.cat_name}</td>
                                <td class="goread" valsc="291|2037554|1">${book.visit_count}</td>
                                <td class="goread" valsc="291|2037554|1">${book.yesterday_buy}</td>
                                <td class="goread">
                                    ${new Date(Date.parse(book.last_index_update_time ? book.last_index_update_time : book.update_time)).Format("yyyy-MM-dd hh:mm")}更新
                                </td>
                                <td class="goread" valsc="291|2037554|1">${book.word_count}</td>
                                <td class="goread" id="opt${book.id}">
                                    <a target="_blank" class="redBtn" href="/author/index_list.html?bookId=${book.id}">章节管理  </a>
                                    <br>
                                    <a target="_blank" href="/author/author_income_detail.html?bookId=${book.id}">薪酬查询  </a>
                                    <br>
                                    <a target="_blank" href="/book/${book.id}.html">作品信息</a>
                                    <br>
                                    <a class="${book.book_status == 0 ? 'redBtn' : ''}" href="javascript:updateBookStatus('${book.id}', '${book.book_status}')">${book.book_status == 0 ? '完结' : '已完结'}  </a>
                                    <br>
                                    <a class="${book.comment_enabled == 0 ? 'redBtn' : ''}" href="javascript:commentOpenClose('${book.id}', '${book.comment_enabled}')">${book.comment_enabled != 1 ? '打开评论区' : '关闭评论区'}  </a>
                                </td>
                            </tr>
                            `;
                        }
                        $("#bookList").html(bookListHtml);

                        layui.use('laypage', function () {
                            var laypage = layui.laypage;

                            //执行一个laypage实例
                            laypage.render({
                                elem: 'shellPage' //注意，这里的 test1 是 ID，不用加 # 号
                                , count: data.count //数据总数，从服务端得到,
                                , curr: data.page
                                , limit: data.limit
                                , jump: function (obj, first) {


                                    //obj包含了当前分页的所有参数，比如：
                                    //console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                                    //console.log(obj.limit); //得到每页显示的条数


                                    //首次不执行
                                    if (!first) {
                                        search(obj.curr, obj.limit);
                                    } else {

                                    }
                                }
                            });
                        });

                    }


                } else if (data.code == 1001) {
                    //未登录
                    location.href = '/user/login.html?originUrl=' + decodeURIComponent(location.href);

                } else {
                    layer.alert(data.msg);
                }

            },
            error: function () {
                layer.alert('网络异常');
            }
        })

    }


    function updateBookStatus(bookId, book_status) {

        $.ajax({
            type: "POST",
            url: "/author/updateBookStatus",
            data: JSON.stringify({ 'bookId': bookId, 'bookStatus': book_status != 1 ? 1 : 0 }),
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {

                    location.reload();


                } else if (data.code == 1001) {
                    //未登录
                    location.href = '/user/login.html?originUrl=' + decodeURIComponent(location.href);

                } else {
                    layer.alert(data.msg);
                }

            },
            error: function () {
                layer.alert('网络异常');
            }
        })

    }


    function commentOpenClose(bookId, commentEnabled) {

        $.ajax({
            type: "POST",
            url: "/author/commentOpenClose",
            data: JSON.stringify({ 'bookId': bookId, 'commentEnabled': commentEnabled == 0 ? 1 : 0 }),
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {

                    location.reload();


                } else if (data.code == 1001) {
                    //未登录
                    location.href = '/user/login.html?originUrl=' + decodeURIComponent(location.href);

                } else {
                    layer.alert(data.msg);
                }

            },
            error: function () {
                layer.alert('网络异常');
            }
        })

    }


    function picChange(bookId) {
        var file = $(`#file${bookId}`).val(); //文件名称
        if (file != "") {
            if (checkPicUpload($(`#file${bookId}`)[0])) {

                $.ajaxFileUpload({
                    url: "/file/picUpload", //用于文件上传的服务器端请求地址
                    secureuri: false, //是否需要安全协议，一般设置为false
                    fileElementId: `file${bookId}`, //文件上传域的ID
                    dataType: "json", //返回值类型 一般设置为json
                    type: "post",
                    success: function (data) { //服务器成功响应处理函数
                        if (data.code == 0) {
                            $.ajax({
                                type: "POST",
                                url: "/author/updateBookPic",
                                data: JSON.stringify({ 'bookId': bookId, 'bookPic': data.data.src }),
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 0) {

                                        location.reload();

                                    } else {
                                        lock = false;
                                        layer.alert(data.msg);
                                    }

                                },
                                error: function () {
                                    lock = false;
                                    layer.alert('网络异常');
                                }
                            });

                        } else {
                            layer.alert(data.msg);
                        }

                    }

                });
            }
        } else {
            alert("请选择上传文件！");
        }



    }
</script>

</html>